<script setup lang="ts">
import { computed, ref } from "vue";
import Copy from "@/components/copy/index.vue"
import JHead from "@/components/j-head/index.vue"
import JNumber from "@/components/j-number/index.vue"
import { formatTimeAgo, formatTimestamp, formatBigNumber } from "@/comm/format";
// import { it } from "node:test";
const PesonURL = '/address/';
const BrowserURL = '/entity/';
const dom = ref();
const props = defineProps({
    pot: Object,
    list: Array<any>,
    page: {
        type: String,
        default: "index"
    }
});

const emit = defineEmits(["hide"]);
const style = computed(() => {
    if (!props.pot || !dom.value) return {};
    console.log("jsz style")
    return {
        left: props.pot.x - dom.value.clientWidth / 2 + "px",
        top: props.pot.y - dom.value.clientHeight - 30 + "px",
    }
})

const onLog = (item: any) => {
    console.log(item);
}

const onHide = () => {
    emit("hide");
}
const findKey = (targetValue: any, item: any) => {
    let key = ''
    for (let i in item) {
        if (targetValue == item[i]) {
            key = i
        }
    }
    return key
}
/* 
    Transfer 优先级
    优先显示eoa的个人to地址 
 */
const indexTransfer = (item: any) => {
    if (item.origin?.address) { return item.origin?.address }
    else if (item.origin?.toAddressType == 'eoa' && item.origin?.toAddress) { return item.origin?.toAddress }
    else if (item.origin?.fromAddressType == 'eoa' && item.origin?.fromAddress) { return item.origin?.fromAddress }
    else if (item.origin?.toTag) { return item.origin.toTag }
    else return item.origin?.fromTag
}
// 首页地址跳转
const indexGetTo = (str: any, item: any) => {
    const key = findKey(str, item.origin)
    console.log('触发首页跳转', key, str)
    if (!key) return
    //无法跳转的key 
    if (key == 'fromTag' || key == 'toTag') return
    if (key == 'address') {
        if (item.origin?.fromAddressType == 'eoa' || item.origin?.toAddressType == 'eoa') {
            window.open(`${PesonURL}${str}`)
        } else {
            if (item.origin?.fromAddressType == 'fund' || item.origin?.fromAddressType == 'cex') {
                window.open(`${BrowserURL}${str}`)
            }
        }
    }
    if (key == 'toAddress') {
        if (item.origin?.toAddressType == 'eoa') {
            window.open(`${PesonURL}${str}`)
        }
    }
    if (key == 'toEntityId') {
        if (item.origin?.toAddressType == 'fund' || item.origin?.toAddressType == 'cex') {
            window.open(`${BrowserURL}${str}`)
        }
    }
}
//机构实体类地址跳转
const browserGetTo = (str: any, item: any) => {
    // console.log('触发机构跳转', key, str, item.origin)
    if (item.origin.relationType == 'eoa' && item.origin?.relationAddress) {
        window.open(`${PesonURL}${item.origin?.relationAddress}`)
    } else if (item.origin?.relationId) {
        if (item.origin?.relationType == 'fund' || item.origin?.relationType == 'cex') {
            window.open(`${BrowserURL}${item.origin?.relationId}`)
        }
    } else if (item.origin?.address) {
        if (item.origin?.addressType == 'eoa') {
            window.open(`${PesonURL}${item.origin?.address}`)
        } else {
            if (item.origin?.addressType == 'fund' || item.origin?.addressType == 'cex') {
                window.open(`${BrowserURL}${item.origin?.address}`)
            }
        }
    } else return
}
//token币种详情跳转
const tokenGetTo = (str: any, item: any) => {
    const key = findKey(str, item.origin)
    console.log('触发token跳转', key, str, item.origin)
    if (key == 'fromAddress') {
        if (item.origin?.fromAddress == 'eoa') {
            window.open(`${PesonURL}${item.origin?.fromAddress}`)
        } else if (!item.origin?.fromAddressType && !item.origin?.toAddressType && !item.origin?.relationType) {
            window.open(`${PesonURL}${item.origin?.fromAddress}`)
        } else return
    }
    if (key == 'toAddress') {
        if (item.origin?.toAddress == 'eoa') {
            window.open(`${PesonURL}${item.origin?.toAddress}`)
        } else if (!item.origin?.fromAddressType && !item.origin?.toAddressType && !item.origin?.relationType) {
            window.open(`${PesonURL}${item.origin?.toAddress}`)
        } else return
    }
}
const personGetTo = (str: any, item: any) => {
    const key = findKey(str, item.origin)
    if (key == 'fromTag' || key == 'toTag') return
    else if (item.origin?.fromAddressType == 'eoa') {
        window.open(`${PesonURL}${item.origin?.fromAddress}`)
    } else if (!item.origin?.fromAddressType && !item.origin?.toAddressType && !item.origin?.relationType) {
        window.open(`${PesonURL}${item.origin?.fromAddress}`)
    } else {
        if (item.origin?.addressType == 'cex' || item.origin?.addressType == 'fund') {
            window.open(`${BrowserURL}${item.origin?.address}`)
        }
    }
}
</script>
<template>
    <div class="table c-title-5 d-flex flex-column tb-more" ref="dom" :style="style">
        <div class="close-box">
            <JHead @onClose="onHide" :text="$t('message.tableLink.a')">
                <template #icon></template>
            </JHead>
        </div>
        <VRow no-gutters class="py-2 pl-2 pr-3 flex-0-1 tb-hds">
            <VCol cols="2"><span>Action</span></VCol>
            <VCol cols="3" class="h-left"><span>Address</span></VCol>
            <VCol cols="2"><span>Amount</span></VCol>
            <VCol cols="3"><span>Value</span></VCol>
            <VCol cols="2"><span>Time</span></VCol>
        </VRow>
        <!-- 首页 -->
        <div class="flex-1-1 overflow-x-hidden" v-if="page == 'index'">
            <VRow no-gutters v-for="item in list" class="py-2 pl-2" @click="onLog(item)">
                <VCol cols="2" class="long-text">
                    <span :style="{ color: item.origin?.action == 'withdraw' ? 'var(--win)' : 'var(--lose)' }">{{
                        item.origin.action }}</span>
                </VCol>
                <!-- address -->
                <VCol cols="3" class="Address h-left">
                    <Copy @change="(e) => indexGetTo(e, item)" :text="indexTransfer(item)" :num="3"
                        v-if="item.origin.action == 'transfer'"></Copy>
                    <!-- 提现都显示to -->
                    <Copy @change="(e) => indexGetTo(e, item)"
                        :text="item.origin.toAddress || item.origin.toEntityId || item.origin.toTag" :num="3"
                        v-else-if="item.origin.action == 'withdraw'"></Copy>
                    <Copy @change="(e) => indexGetTo(e, item)" :text="item.origin.address || item.origin.fromTag" :num="3"
                        v-else-if="item.origin.action == 'deposit'"></Copy>
                    <Copy @change="(e) => indexGetTo(e, item)" :text="item.origin.address || item.origin.fromTag" :num="3"
                        v-else></Copy>
                </VCol>
                <!-- Amount  -->
                <VCol cols="2">
                    <span v-if="item.origin.action == 'withdraw'">{{
                        formatBigNumber(Math.abs(item.origin.outAmount)) }}
                    </span>
                    <span v-else-if="item.origin.action == 'deposit'">{{
                        formatBigNumber(item.origin.inAmount || item.origin.outAmount) }}</span>
                    <span v-else>{{ formatBigNumber(item.origin.outAmount) }}</span>
                </VCol>
                <!-- value -->
                <VCol cols="3">$<JNumber :num="item.origin.outusdt || item.origin.usd"></JNumber>
                </VCol>
                <!--  time -->
                <VCol cols="2"><span class="tet">{{ formatTimeAgo(item.origin.timestamp) }}</span></VCol>
            </VRow>
        </div>
        <!-- 机构 -->
        <div class="flex-1-1 overflow-x-hidden" v-if="page == 'browser'">
            <VRow no-gutters v-for="item in list" class="py-2 pl-2" @click="onLog(item)">
                <VCol cols="2" class="long-text">
                    <span :style="{ color: item.origin?.action == 'withdraw' ? 'var(--win)' : 'var(--lose)' }">{{
                        item.origin.action }}</span>
                </VCol>
                <!-- address -->
                <VCol cols="3" class="Address h-left">
                    <!-- 转账 -->
                    <Copy @change="(e) => browserGetTo(e, item)" :text="item.origin.relationAddress || item.origin.address"
                        :num="3" v-if="item.origin.action == 'transfer'"></Copy>
                    <!-- 收到/发送 -->
                    <Copy @change="(e) => browserGetTo(e, item)" :text="item.origin.relationAddress" :num="3"
                        v-else-if="item.origin.action == 'receive' || item.origin.action == 'send'"> </Copy>
                    <!-- 提现  -->
                    <Copy @change="(e) => browserGetTo(e, item)" :text="item.origin.relationAddress" :num="3"
                        v-else-if="item.origin.action == 'withdraw'"></Copy>
                    <!-- 充值 -->
                    <Copy @change="(e) => browserGetTo(e, item)" :text="item.origin.relationAddress" :num="3"
                        v-else-if="item.origin.action == 'deposit'"> </Copy>
                    <!-- 其他 -->
                    <Copy @change="(e) => browserGetTo(e, item)"
                        :text="item.origin.address || item.origin.fromTag || item.origin.toTag" :num="3" v-else> </Copy>
                </VCol>
                <!-- Amount  -->
                <VCol cols="2">
                    <span v-if="item.origin.action == 'withdraw'">{{
                        formatBigNumber(Math.abs(item.origin.outAmount || item.origin.inAmount)) }}
                    </span>
                    <span v-else-if="item.origin.action == 'deposit'">{{
                        formatBigNumber(item.origin.inAmount) }}
                    </span>
                    <span v-else-if="item.origin.action == 'receive'">{{
                        formatBigNumber(item.origin.inAmount) }}
                    </span>
                    <span v-else>{{ formatBigNumber(item.origin.outAmount) }}</span>
                </VCol>
                <!-- value -->
                <VCol cols="3">$<JNumber :num="item.origin.outusdt || item.origin.usd"></JNumber>
                </VCol>
                <!--  time -->
                <VCol cols="2"><span class="tet">{{ formatTimeAgo(item.origin.timestamp) }}</span></VCol>
            </VRow>
        </div>
        <!-- token详情 -->
        <div class="flex-1-1 overflow-x-hidden" v-if="page == 'token'">
            <VRow no-gutters v-for="item in list" class="py-2 pl-2" @click="onLog(item)">
                <VCol cols="2" class="long-text">
                    <span :style="{ color: item.origin?.action == 'withdraw' ? 'var(--win)' : 'var(--lose)' }">{{
                        item.origin.action }}</span>
                </VCol>
                <!-- address -->
                <!-- token的数据会出现三个type为空的情况 视为个人转账  -->
                <VCol cols="3" class="Address h-left">
                    <!-- 转账 -->
                    <Copy @change="(e) => tokenGetTo(e, item)" :text="item.origin.fromAddress" :num="3"
                        v-if="item.origin.action == 'transfer'"></Copy>
                    <!-- 提现 -->
                    <Copy @change="(e) => tokenGetTo(e, item)" :text="item.origin.toAddress || item.origin.toTag" :num="3"
                        v-else-if="item.origin.action == 'withdraw'"></Copy>
                    <!-- 充值 -->
                    <Copy @change="(e) => tokenGetTo(e, item)" :text="item.origin.fromAddress || item.origin.fromTag"
                        :num="3" v-else-if="item.origin.action == 'deposit'"> </Copy>
                    <!-- 其他 -->
                    <Copy @change="(e) => tokenGetTo(e, item)" :text="item.origin.toAddress || item.origin.toTag" :num="3"
                        v-else> </Copy>
                </VCol>
                <!-- Amount  -->
                <VCol cols="2">
                    <span v-if="item.origin.action == 'withdraw'">{{
                        formatBigNumber(Math.abs(item.origin.outAmount)) }}
                    </span>
                    <span v-else-if="item.origin.action == 'deposit'">{{
                        formatBigNumber(item.origin.inAmount) }}
                    </span>
                    <span v-else>{{ formatBigNumber(item.origin.outAmount) }}</span>
                </VCol>
                <!-- value -->
                <VCol cols="3">$<JNumber :num="item.origin.outusdt || item.origin.usd"></JNumber>
                </VCol>
                <!--  time -->
                <VCol cols="2"><span class="tet">{{ formatTimeAgo(item.origin.timestamp) }}</span></VCol>
            </VRow>
        </div>

        <!-- 个人 -->
        <div class="flex-1-1 overflow-x-hidden" v-if="page == 'person'">
            <VRow no-gutters v-for="item in list" class="py-2 pl-2" @click="onLog(item)">
                <VCol cols="2" class="long-text">
                    <span :style="{ color: item.origin?.action == 'withdraw' ? 'var(--win)' : 'var(--lose)' }">{{
                        item.origin.action }}</span>
                </VCol>
                <!-- address -->
                <VCol cols="3" class="Address h-left">
                    <!-- 个人数据to都是向本人地址 所有均显示from  -->
                    <Copy @change="(e) => personGetTo(e, item)" :text="item.origin.fromAddress || item.origin.fromTag"
                        :num="3"> </Copy>
                </VCol>
                <!-- Amount  -->
                <VCol cols="2">
                    <span v-if="item.origin.action == 'withdraw'">{{
                        formatBigNumber(Math.abs(item.origin.outAmount || item.origin.inAmount)) }}
                    </span>
                    <span v-else>{{ formatBigNumber(item.origin.inAmount) }}</span>
                </VCol>
                <!-- value -->
                <VCol cols="3">$<JNumber :num="item.origin.outusdt || item.origin.usd"></JNumber>
                </VCol>
                <!--  time -->
                <VCol cols="2"><span class="tet">{{ formatTimeAgo(item.origin.timestamp) }}</span></VCol>
            </VRow>
        </div>
    </div>
</template>
<style lang="less" scoped>
.long-text {
    overflow: hidden;
    text-wrap: nowrap;
    text-overflow: ellipsis;
}

.table {
    width: 360px;
    height: 207px;
    border-radius: 4px;
    background-color: #21252B;
    box-shadow: 3px 4px 4px rgba(0, 0, 0, 0.5);

    // position: fixed;
    // left: 20%;
    img {
        width: 16px;
        height: 16px;
    }

    .header {
        display: flex;
    }

    .v-row {
        border-bottom: 1px solid var(--border_);
    }

    .v-col {
        padding-block: 0;
    }

    .v-col:nth-child(2) {
        text-align: right;
    }

    .v-col:nth-child(3) {
        padding-left: 0px;
        text-align: center;
    }

    .v-col:nth-child(4) {
        // padding-left: 30px;
        text-align: center;
    }

    .Address {
        overflow: hidden;
        text-wrap: nowrap;
        text-overflow: ellipsis;
    }

    .h-left {
        text-align: left !important;
    }

    .v-col:last-child {
        text-align: right;
    }
}

.tet {
    text-wrap: nowrap;
    margin-right: 6px;
}

.tb-more {
    position: absolute;
    z-index: 3;
    left: -1200px;
    top: 0;
}

.tb-hds {
    color: #B1B6C6;
}

.close-box {
    width: 100%;

    .head {
        width: 100%;
    }

    :deep(.ml-2) {
        margin-left: 0 !important;
    }

    // background-color: red;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    border-bottom: 1px solid #2B3139;
    ;

    .close-btn {
        width: 12px;
        height: 12px;
        object-fit: contain;
        margin-right: 8px;
    }

    .tt {
        font-weight: 600;
        color: #FFFFFF;
    }
}

// .h-center{
//     text-align: center !important;
// }
</style>
